<?php
/**
 * User:于小涛
 * Date:2020/2/18
 * Time:16:31
 */
namespace app\admin\business\message;
use app\admin\model\rbac\AdminRole;
use app\admin\model\timer\Timer;
use think\facade\Db;

class Message{

    private $obj_admin_role_model=null;
    private $obj_timer_model = null;
    private $str_timer_name='发送统计数据';
    public function __construct()
    {
        $this->obj_admin_role_model = new AdminRole();
    }

    /**
     * 发送消息，通知还没有录入当天数据的人员录入数据
     * User:于小涛
     * Date:2020/10/8
     * Time:15:54
     */
    public function SendAddData(){
        //查询当天还没有录入数据的推广专员和创业顾问
        $str_today=date("Y-m-d");
        $str_sql="select ra.* from rbac_admin as ra left join rbac_admin_role as rar on ra.id=rar.admin_id where rar.role_id in(".config('project.role_id.tuiguangzhuanyuan.id').",".config('project.role_id.chuangyeguwen.id').") and ra.is_lock=1 and ra.is_del=1 and ra.id not in(select tuiguang_admin_id from promotion_record where date='{$str_today}' and tuiguang_is_add=2) and ra.id not in(select chuangye_admin_id from promotion_record where date='{$str_today}' and chuangye_is_add=2)";
        $arr_admin_list=Db::query($str_sql);
        if(empty($arr_admin_list)){
            trace("所有人员都已经录入完毕，不需要发送通知");
        }else{
            foreach($arr_admin_list as $arr_item){
                $arr_message_content=array(
                    'first'=>array(
                        'value'=>'数据录入提醒',
                        'color'=>'#173177',
                    ),
                    'keyword1'=>array(
                        'value'=>'系统提醒',
                        'color'=>'#173177',
                    ),
                    'keyword2'=>array(
                        'value'=>'今日数据还未录入，请立即点击录入',
                        'color'=>'#173177',
                    ),
                    'remark'=>array(
                        'value'=>'',
                        'color'=>'#173177',
                    ),
                );
                $str_miniprogram='';
                $this->_addMessage(config('project.message_send_add_data_temp_id'),$arr_item['wechat_unionid'],$arr_item['name'],$arr_message_content,'当日数据未录入',$str_miniprogram);
            }
        }
    }
    private function _addMessage($str_temp_id,$str_unionid,$str_user_name,$arr_message_content,$str_message_name='',$str_miniprogram=''){
        $str_url=config("project.message_push_api");
        $arr_post_data=array(
            'platform_code'=>config('project.message_push_platform_code'),
            'message_name'=>$str_message_name,
            'template_id'=>$str_temp_id,
            'touser'=>$str_unionid,
            'url'=>'',
            'miniprogram'=>$str_miniprogram,
//            'miniprogram'=>json_encode(array(
//                "appid"=>C('appid'),
//                "pagepath"=>"/pages/index/index",
//            )),
            'data'=>json_encode($arr_message_content),
            'touser_name'=>$str_user_name,
        );
        trace("给消息推送平台发送消息，地址：{$str_url},数据：".var_export($arr_post_data,true),'info');
        $arr_platform_re=curl_post($str_url,$arr_post_data);
        if($arr_platform_re['ret']==0){
            trace("推送到平台成功返回：".var_export($arr_platform_re,true),'info');
        }else{
            trace("推送到平台失败返回：".var_export($arr_platform_re,true),'error');
        }
        return $arr_platform_re;
    }
    /**
     * 给统计结果查看人员发送当天输入已经录入完毕的消息
     * User:于小涛
     * Date:2020/10/8
     * Time:15:08
     */
    public function sendStatisticData(){
        //查询所有的需要查看统计数据的人员
        $str_sql="select ra.* from rbac_admin as ra left join rbac_admin_role as rar on rar.admin_id=ra.id where rar.role_id=".config('project.role_id.statistic.id');
        $arr_admin_list=Db::query($str_sql);
        foreach($arr_admin_list as $arr_item){
            $arr_message_content=array(
                'first'=>array(
                    'value'=>'数据录入完毕',
                    'color'=>'#173177',
                ),
                'keyword1'=>array(
                    'value'=>'点击查看宜盟当日统计数据',
                    'color'=>'#173177',
                ),
                'keyword2'=>array(
                    'value'=>date("Y-m-d H:i:s"),
                    'color'=>'#173177',
                ),
                'remark'=>array(
                    'value'=>'',
                    'color'=>'#173177',
                ),
            );
            $str_miniprogram='';
            $this->_addMessage(config('project.message_send_statistic_data_temp_id'),$arr_item['wechat_unionid'],$arr_item['name'],$arr_message_content,'当日数据录入完毕',$str_miniprogram);
        }
    }
}